/**
 * 
 */
package de.cbf.cam.dsa.battle.ui.listeners;

import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;

/**
 * @author Nebelritter
 *
 */
public class EnterRangedNumbers extends EnterNumbers {
	/**
	 * @uml.property  name="min"
	 */
	private int min = 0;
	/**
	 * @uml.property  name="max"
	 */
	private int max = 0;
	
	public EnterRangedNumbers(int min, int max) {
		super();
		this.min = min;
		this.max = max;
	}

	@Override
	public void verifyText(VerifyEvent e) {
		super.verifyText(e);
		Widget widget =e.widget;
		if (widget instanceof Text) {
			Text text = (Text) widget;			
			//add the text to the already given text
			String there = text.getText();
			String toAdd = e.text;
			String test  = there+toAdd;			 
			try{
				int value=Integer.parseInt(test);
				if(value<min||max<value){
					e.doit=false;
				}
			}catch (NumberFormatException exc) {
				//..but accept empty values
				if(test.length()!=0){
					e.doit=false;
				}				
			}				
		}
	}	
	
	public static EnterRangedNumbers getInstance(int min, int max){		
		return new EnterRangedNumbers(min,max);
	}
	
}
